package com.sxhuayuan.parking.controller.member;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.compenent.web.RespMessage.Status;
import com.sxhuayuan.parking.entity.Member;
import com.sxhuayuan.parking.entity.MemberCarPlate;
import com.sxhuayuan.parking.service.MemberCarPlateService;
import com.sxhuayuan.parking.service.MemberService;
import com.sxhuayuan.parking.utils.RegexpUtil;
import com.sxhuayuan.parking.utils.SettingUtils;
import com.sxhuayuan.parking.utils.SpringUtils;

@Controller("memberMemberCarPlateController")
@RequestMapping("/member/carplate")
public class MemberCarPlateController extends BaseController {

	@Resource(name = "memberCarPlateServiceImpl")
	private MemberCarPlateService service;

	@Resource(name = "memberServiceImpl")
	private MemberService memberService;

	/**
	 * 增加车牌列表路由
	 * 
	 * @return 车牌列表页面
	 */
	@GetMapping(value = "/list")
	public String getList() {
		return "plateList/plate_list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public RespMessage getCarPlate() {
		Member member = memberService.getCurrent();
		List<MemberCarPlate> carPlateList = service.findList(member.getId());
		return RespMessage.builder().success().setDatas(carPlateList).build();
	}

	/** 添加车牌页面 */
	@RequestMapping(value = "/bind", method = RequestMethod.GET)
	public String binding(Model model) {
		Member member = memberService.getCurrent();
		Integer maxCarPlateCount = SettingUtils.get().getMaxCarPlateCount();
		if (service.findCount(member.getId()) >= maxCarPlateCount) {
			addErrorMessage(SpringUtils.getMessage(Message.CAR_PLATE_MAX_COUNT_LIMIT, maxCarPlateCount));
			return "plateList/plate_list";
		}
		return "binding/binding";
	}

	/**
	 * 绑定车牌
	 */
	@RequestMapping(value = "/bind", method = RequestMethod.POST)
	@ResponseBody
	public RespMessage bindCarPlate(@RequestBody Map<String, String> data) {

		String carNum = data.get("carNum");
		if (!RegexpUtil.isCarPlate(carNum))
			return RespMessage.error("车牌错误，请检查");
		carNum = carNum.toUpperCase();
		Member member = memberService.getCurrent();
		Integer maxCarPlateCount = SettingUtils.get().getMaxCarPlateCount();
		if (service.findCount(member.getId()) >= maxCarPlateCount) {
			return RespMessage.error(Message.CAR_PLATE_MAX_COUNT_LIMIT, maxCarPlateCount);
		}
		/** 绑定是根据车牌号 还有member 存在不绑定 */
		MemberCarPlate carPlate = service.findByMemberIdAndCarPlate(member.getId(), carNum);
		if (Objects.isNull(carPlate)) {
			Boolean isDef = Boolean.valueOf(data.get("isDef"));
			carPlate = new MemberCarPlate();
			carPlate.setMember(member);
			carPlate.setCarPlate(carNum);
			service.save(carPlate);
			if (isDef)
				service.setDefault(member, carPlate.getId());
		} else {
			return RespMessage.error(Status.VALID_ERR, Message.CAR_PLATE_IS_BINDING);
		}
		return RespMessage.success(Message.CAR_PLATE_BINDING_SUCCESS);
	}

	/** 新增车牌 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public RespMessage delete(Long carPlateId) {
		Member member = memberService.getCurrent();
		MemberCarPlate cp = service.findOne(carPlateId);
		if (!cp.getMember().equals(member)) {
			return RespMessage.error(Message.CAR_PLATE_NOT_EXISTS);
		}
		try {
			service.delete(cp);
		} catch (Exception e) {
			return RespMessage.error("删除失败:" + e.getMessage());
		}
		return RespMessage.success();
	}

	/** 设置默认车牌 */
	@RequestMapping(value = "/setdef")
	@ResponseBody
	public RespMessage setDef(Long carPlateId) {
		Member member = memberService.getCurrent();
		MemberCarPlate cp = service.findOne(carPlateId);
		if (!cp.getMember().equals(member)) {
			return RespMessage.error(Message.CAR_PLATE_NOT_EXISTS);
		}
		try {
			service.setDefault(member, carPlateId);
		} catch (Exception e) {
			return RespMessage.error("设置失败:" + e.getMessage());
		}
		return RespMessage.success();
	}
}
